package com.levy.utils;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ImageUtil;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by levy on 2019/7/1.
 */
public class ImageUtils {

    @Test
    public void test() throws IOException {
        File srcFile = new File("G:\\1.jpg");
        String fileName = srcFile.getName();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        int[] rgb = new int[3];
        BufferedImage bi = ImageIO.read(srcFile);

        int width = bi.getWidth();
        int height = bi.getHeight();
        int minx = bi.getMinX();
        int miny = bi.getMinY();
        System.out.println("正在处理：" + srcFile.getAbsolutePath());

        for (int i = minx; i < width; i++) {
            for (int j = miny; j < height; j++) {
                int pixel = bi.getRGB(i, j);
                // 分别进行位操作得到 r g b上的值
                rgb[0] = (pixel & 0xff0000) >> 16;
                rgb[1] = (pixel & 0xff00) >> 8;
                rgb[2] = (pixel & 0xff);
                if (rgb[0] > 175 && rgb[0] < 217
                        && rgb[1] > 181 && rgb[1] < 221
                        && rgb[2] > 189 && rgb[2] < 232) {
                    bi.setRGB(i, j, 0xFF0000);
                }
            }
        }

        // 将缓冲对象保存到新文件中
        File desFile = new File("G:\\2.jpg");
        FileOutputStream ops = new FileOutputStream(desFile);
        ImageIO.write(bi, suffix, ops);
        ops.flush();
        ops.close();
        System.out.println("\t处理完毕：" + desFile.getAbsolutePath());
        System.out.println();
    }


    /**
     * 去除水印
     *
     * @param srcFilePath
     * @param desFilePath
     * @throws IOException
     */
    public static void removeWaterMark(String srcFilePath, String desFilePath) throws IOException {
        File srcFile = new File(srcFilePath);
        String fileName = srcFile.getName();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        int[] rgb = new int[3];
        BufferedImage bi = ImageIO.read(srcFile);

        int width = bi.getWidth();
        int height = bi.getHeight();
        int minx = bi.getMinX();
        int miny = bi.getMinY();
        System.out.println("正在处理：" + srcFile.getAbsolutePath());

        for (int i = minx; i < width; i++) {
            for (int j = miny; j < height; j++) {
                int pixel = bi.getRGB(i, j);
                // 分别进行位操作得到 r g b上的值
                rgb[0] = (pixel & 0xff0000) >> 16;
                rgb[1] = (pixel & 0xff00) >> 8;
                rgb[2] = (pixel & 0xff);
                if (rgb[0] > 204 && rgb[0] < 209
                        && rgb[1] > 204 && rgb[1] < 209
                        && rgb[2] > 204 && rgb[2] < 209) {
                    bi.setRGB(i, j, 0xffffff);
                }
            }
        }

        // 将缓冲对象保存到新文件中
        File outFilePath = new File(desFilePath);
        if (!outFilePath.exists()) {
            outFilePath.mkdir();
        }
        File desFile = new File(desFilePath + File.separator + fileName);
        FileOutputStream ops = new FileOutputStream(desFile);
        ImageIO.write(bi, suffix, ops);
        ops.flush();
        ops.close();
        System.out.println("\t处理完毕：" + desFile.getAbsolutePath());
        System.out.println();
    }


    /**
     * 去除水印
     *
     * @param srcDir
     * @param desDir
     */
    public static void removeWaterMarkBatch(String srcDir, String desDir) {
        try {
            File dir = new File(srcDir);
            String[] list = dir.list();
            for (String fileName : list) {
                removeWaterMark(srcDir + File.separator + fileName, desDir);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void addFontWaterMark(String srcImg,String desImg,String fontMark) {
        ImgUtil.pressText(//
                FileUtil.file(srcImg), //
                FileUtil.file(desImg), //
                fontMark, Color.BLACK, //文字
                new Font("黑体", Font.BOLD, 100), //字体
                0, //x坐标修正值。 默认在中间，偏移量相对于中间偏移
                0, //y坐标修正值。 默认在中间，偏移量相对于中间偏移
                0.8f//透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字
        );
    }


    public static void addImageWaterMark(String srcImg, String desImg, String waterMarkImg) {
        ImgUtil.pressImage(
                FileUtil.file(srcImg),
                FileUtil.file(desImg),
                ImgUtil.read(waterMarkImg), //水印图片
                0, //x坐标修正值。 默认在中间，偏移量相对于中间偏移
                0, //y坐标修正值。 默认在中间，偏移量相对于中间偏移
                0.1f
        );
    }


    public static void main(String args[]) throws IOException, IOException {
        String srcDir = "C:\\Users\\levy\\Desktop\\111";
        String desDir = "C:\\Users\\levy\\Desktop\\222";
        removeWaterMarkBatch(srcDir, desDir);
    }

}
